package com.tanhao.takeaway.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tanhao.takeaway.commom.BaseContext;
import com.tanhao.takeaway.commom.R;
import com.tanhao.takeaway.entity.ShoppingCart;
import com.tanhao.takeaway.entity.dish;
import com.tanhao.takeaway.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        shoppingCart.setUserId(BaseContext.getThreadLocalId());
        shoppingCart.setUpdateTime(LocalDateTime.now());
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper=new LambdaQueryWrapper<>();

        if(dishId!=null){
           lambdaQueryWrapper.eq(ShoppingCart::getDishId,dishId);

        } else {
            lambdaQueryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        ShoppingCart one = shoppingCartService.getOne(lambdaQueryWrapper);

        if(one!=null){
            Integer number = one.getNumber();
            one.setNumber(number+1);
             shoppingCartService.updateById(one);
        }else {
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            one=shoppingCart;
        }

        return R.success(one);
    }

    @PostMapping("/sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart){
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(shoppingCart.getDishId()!=null,ShoppingCart::getDishId,shoppingCart.getDishId());
        lambdaQueryWrapper.eq(ShoppingCart::getUserId,BaseContext.getThreadLocalId());
        ShoppingCart one = shoppingCartService.getOne(lambdaQueryWrapper);

        Integer dishNumber = one.getNumber();
        if(dishNumber==1){
            shoppingCartService.remove(lambdaQueryWrapper);
        } else {
            one.setNumber(dishNumber-1);
            shoppingCartService.updateById(one);
        }
        return R.success("减去商品成功");
    }

    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId,BaseContext.getThreadLocalId());
        lambdaQueryWrapper.orderByAsc(ShoppingCart::getUpdateTime);
        List<ShoppingCart> list = shoppingCartService.list(lambdaQueryWrapper);
        return R.success(list);
    }

    @DeleteMapping("clean")
    public R<String> delete(){
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId,BaseContext.getThreadLocalId());
        shoppingCartService.remove(lambdaQueryWrapper);
        return R.success("成功清空购物车");
    }
}
